/*
 * Copyright(C) 2022. Huawei Technologies Co.,Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

APP_ERROR MxpiOpenposePostProcess::GenerateMxpiOutput(const std::vector<std::vector<PartPair>> &personList,
                                                      mxpiopenposeproto::MxpiPersonList &dstMxpiPersonList)
{
    const float floatEqualZeroBias = 0.000001;
    ofstream outfile;
    outfile.open("output1.txt", ios::app);
    for (int k = 0; k < personList.size(); k++)
    {
        float personScore = PersonScore(personList[k]);
        // Ignore person with score 0
        if (fabs(personScore - 0) < floatEqualZeroBias)
        {
            continue;
        }
        auto mxpiPersonPtr = dstMxpiPersonList.add_personinfovec();
        mxpiopenposeproto::MxpiMetaHeader *dstPersonMxpiMetaheaderList = mxpiPersonPtr->add_headervec();
        dstPersonMxpiMetaheaderList->set_datasource(parentName_);
        dstPersonMxpiMetaheaderList->set_memberid(0);
        mxpiPersonPtr->set_score(personScore);
        for (int j = 0; j < personList[k].size(); j++)
        {
            PartPair skeleton = personList[k][j];
            auto mxpiSkeletonPtr = mxpiPersonPtr->add_skeletoninfovec();
            mxpiSkeletonPtr->set_cocoskeletonindex1(skeleton.partIdx1);
            mxpiSkeletonPtr->set_cocoskeletonindex2(skeleton.partIdx2);
            mxpiSkeletonPtr->set_x0(skeleton.coord1[0]);
            mxpiSkeletonPtr->set_y0(skeleton.coord1[1]);
            mxpiSkeletonPtr->set_point1score(skeleton.score1);
            mxpiSkeletonPtr->set_x1(skeleton.coord2[0]);
            mxpiSkeletonPtr->set_y1(skeleton.coord2[1]);
            mxpiSkeletonPtr->set_point2score(skeleton.score2);
            mxpiSkeletonPtr->set_skeletonscore(skeleton.score);
            outfile << skeleton.score << "\n";
            outfile.close();
        }
    }
    return APP_ERR_OK;
}